package org.example.mynote.controller;


import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.mynote.dto.LoginRequestDTO;
import org.example.mynote.dto.LoginResponseDTO;
import org.example.mynote.dto.RegisterRequestDTO;
import org.example.mynote.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/user")
@Slf4j
@Tag(name = "员工相关接口")

public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册接口
     *
     * @param registerRequestDTO
     * @return
     */
    @PostMapping("/register")
    public void register(@RequestBody RegisterRequestDTO registerRequestDTO) {
        userService.register(registerRequestDTO.getUsername(), registerRequestDTO.getPassword());
    }

    @PostMapping("/login")
    public ResponseEntity<?> loginUser(@RequestBody LoginRequestDTO loginRequestdto) {
        try {
            // 调用 service 方法，现在它返回的是一个 LoginResponse 对象
            LoginResponseDTO loginResponsedto = userService.login(loginRequestdto.getUsername(), loginRequestdto.getPassword());

            // 直接将这个包含 token, userId, username 的对象返回
            return ResponseEntity.ok(loginResponsedto);

        } catch (RuntimeException e) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
        }
    }
}
